<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="12">
          <a-col :md="6" :sm="8">
            <a-form-item label="标题" :labelCol="{ span: 6 }" :wrapperCol="{ span: 14, offset: 1 }">
              <a-input placeholder="标题" v-model="queryParam.title" allowClear></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-item label="分类" :labelCol="{ span: 6 }" :wrapperCol="{ span: 14, offset: 1 }">
            <a-cascader 
              :options="flList" 
              placeholder="请选择" 
              v-model="categoryId" 
              allowClear
              :field-names="{ label: 'name', value: 'id', children: 'categoryList' }"
            />
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-item label="状态" :labelCol="{ span: 6 }" :wrapperCol="{ span: 14, offset: 1 }">
              <a-select v-model="queryParam.status" placeholder="请选择" default-value="null" allowClear>
                <a-select-option v-for="(item, index) in statusList" :key="index" :value="item.value">
                  {{ item.name }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
      <div class="table-operator" style="border-top: 5px; margin-bottom: 20px">
        <a-button type="primary" @click="handleAdd" style="margin-right: 10px">新增</a-button>
      </div>

      <a-table ref="table" rowKey="id" size="middle" :columns="columns" :dataSource="dataSource" :pagination="ipagination" :loading="loading" @change="handleTableChange">
        <span slot="img" slot-scope="text">
          <img :src="text" alt="" style="width: 100px; height: 100px" />
        </span>
        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)"><a-icon type="setting" /> 编辑</a>
          <a-divider type="vertical" />
          <a-popconfirm title="是否禁用" ok-text="确认" cancel-text="取消" @confirm="disableclick(record)">
            <a href="#">{{ record.status == 1 ? '启用' : '禁用' }}</a>
          </a-popconfirm>
          <a-divider type="vertical" />
          <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
            <a>删除</a>
          </a-popconfirm>
        </span>
      </a-table>
    </div>

    <contentModel ref="modalForm" @ok="modalFormOk"></contentModel>
  </a-card>
</template>

<script>
import { filterObj } from '@/utils/util'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { queryCategoryList, updateContentStatus } from '@/api/marketingCenter/dealer'
import contentModel from './modules/contentModel.vue'

export default {
  name: 'content',
  mixins: [JeecgListMixin],
  components: {
    contentModel
  },
  data() {
    return {
      statusList: [
        { name: '启用', value: '0' },
        { name: '禁用', value: '1' }
      ],
      queryParam: {
        current: 1,
        size: 10
      },
      // 表头
      columns: [
        {
          title: '序号',
          dataIndex: '',
          key: 'rowIndex',
          width: 120,
          align: 'center',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '封面图',
          dataIndex: 'coverUrl',
          align: 'center',
          scopedSlots: { customRender: 'img' }
        },
        {
          title: '标题',
          align: 'center',
          dataIndex: 'title'
        },
        {
          title: '分类',
          align: 'center',
          dataIndex: 'categoryName'
        },
        {
          title: '排序',
          align: 'center',
          dataIndex: 'sort'
        },
        {
          title: '状态',
          align: 'center',
          dataIndex: 'statusText'
        },
        {
          title: '创建时间',
          align: 'center',
          dataIndex: 'createTime'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          scopedSlots: { customRender: 'action' }
        }
      ],
      flList: [],
      categoryId: [],
      url: {
        list: '/backend/dealer/pageContentVo',
        delete: '/backend/dealer/delContentById'
      }
    }
  },
  created() {
    this.getqueryCategoryList()
  },
  methods: {
    getQueryParams() {
      var param = Object.assign({}, this.queryParam)
      param.current = this.ipagination.current
      param.size = this.ipagination.pageSize
      if (this.categoryId.length !== 0) {
        param.categoryId = this.categoryId[this.categoryId.length - 1]
      }
      return filterObj(param)
    },
    disableclick(record) {
      let dealerParam = {
        status: record.status ? 0 : 1,
        id: record.id
      }
      updateContentStatus(dealerParam).then(res => {
        if (res.code == 200) {
          this.$message.success(res.msg)
          this.searchQuery()
        } else {
          this.$message.warning(res.msg)
        }
      })
    },
    getqueryCategoryList() {
      queryCategoryList().then(res => {
        if (res.code == 200) {
          this.flList = res.data
        }
      })
    }
  }
}
</script>
